package com.iseastar.dianxiaosan.coupon;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.CouponBean;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import java.util.ArrayList;

import droid.frame.view.xlist.FooterListener;
import droid.frame.view.xlist.SListView;

/**
 * 邮票（已使用、已过期）列表
 *
 * @author coffee<br>
 *         2015-7-2下午2:38:31
 */
public class CouponExpireListActivity extends BaseActivity2 {

    private SListView mListView;
    private CouponListAdapter mAdapter;

    private ReqResult<CouponBean> result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // showLoadingBar();
        showLoadingDialog(null);
        AppHttp.getInstance().queryCouponDisableList(MsgID.couponList + 2);

    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.coupon_list);
        super.findViewById();
        getAppTitle().setCommonTitle("失效邮票");
        mListView = (SListView) findViewById(R.id.listview);
        //
        ArrayList<CouponBean> items = new ArrayList<CouponBean>();
        mAdapter = new CouponListAdapter(items, getContext());
        // mAdapter.setShowFooter(true);

        mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().queryCouponDisableList(MsgID.couponList + 2);
            }
        });
        //
        mListView.setFooterListener(new FooterListener() {
            @Override
            public void onFooterVisible() {
                if (result.getPageNum() < result.getTotalPage()) {
                    AppHttp.getInstance().queryCouponDisableList(MsgID.couponList + 2);
                }
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.couponList + 2:
                cancelLoadingDialog();
                onRefreshComplete();
                result = JSON.parseList(msg.obj, CouponBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mListView.getAdapter() == null) {
                                mListView.setAdapter(mAdapter);
                            }
                            if (result.getPageNum() <= 1) {
                                mAdapter.setItems(result.getResultList(), true);
                            } else {
                                mAdapter.appendItems(result.getResultList(), true);
                            }
                            mListView.updateFooter(result.getPageNum(), result.getTotalPage());
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }
}
